package bitcat.chuenyee.utils

import bitcat.chuenyee.utils.YmlUtil.yaml
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.io.StringReader
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class YmlUtilTest {

    @Test
    fun `test valid yaml multiline with proper indentation`() {
        val yamlStr = """
            key1: value1
            key2:
              subkey1: subvalue1
              subkey2: subvalue2
            key3:
              - item1
              - item2
        """.trimIndent()

        val result = YmlUtil.parseYamlStringToMap(yamlStr)
        assertEquals("value1", result["key1"])
        assertEquals(mapOf("subkey1" to "subvalue1", "subkey2" to "subvalue2"), result["key2"])
        assertEquals(listOf("item1", "item2"), result["key3"])
    }

    @Test
    fun `test valid yaml  with  n`() {
        val yamlStr = "key1: value1\nkey2:\n  subkey1: subvalue1\n  subkey2: subvalue2\nkey3:\n  - item1\n  - item2"

        val result = YmlUtil.parseYamlStringToMap(yamlStr)
        assertEquals("value1", result["key1"])
        assertEquals(mapOf("subkey1" to "subvalue1", "subkey2" to "subvalue2"), result["key2"])
        assertEquals(listOf("item1", "item2"), result["key3"])
    }

    @Test
    fun `test valid yaml  with  Windows  (CRLF)`() {
        val yamlStrWindows = "key1: value1\r\nkey2:\r\n  subkey1: subvalue1\r\n  subkey2: subvalue2\r\nkey3:\r\n  - item1\r\n  - item2"
        val result = YmlUtil.parseYamlStringToMap(yamlStrWindows)
        assertEquals("value1", result["key1"])
        assertEquals(mapOf("subkey1" to "subvalue1", "subkey2" to "subvalue2"), result["key2"])
        assertEquals(listOf("item1", "item2"), result["key3"])
    }

    @Test
    fun `test valid yaml  with  LinuxmacOS  (LF)`() {
        val yamlStrLinux = "key1: value1\nkey2:\n  subkey1: subvalue1\n  subkey2: subvalue2\nkey3:\n  - item1\n  - item2"
        val result = YmlUtil.parseYamlStringToMap(yamlStrLinux)
        assertEquals("value1", result["key1"])
        assertEquals(mapOf("subkey1" to "subvalue1", "subkey2" to "subvalue2"), result["key2"])
        assertEquals(listOf("item1", "item2"), result["key3"])
    }

    @Test
    fun `test valid yaml  with  macOS  (CR)`() {
        val yamlStrOldMac = "key1: value1\rkey2:\r  subkey1: subvalue1\r  subkey2: subvalue2\rkey3:\r  - item1\r  - item2"
        val result = YmlUtil.parseYamlStringToMap(yamlStrOldMac)
        assertEquals("value1", result["key1"])
        assertEquals(mapOf("subkey1" to "subvalue1", "subkey2" to "subvalue2"), result["key2"])
        assertEquals(listOf("item1", "item2"), result["key3"])
    }

    @Test
    fun `test valid yaml  with emptyString`() {
        val result = YmlUtil.parseYamlStringToMap("")
        assertEquals(result, emptyMap<String, Any>())
    }

    @Test
    fun `test valid yaml  with null string`() {
        val result = YmlUtil.parseYamlStringToMap("null")
        assertEquals(result, emptyMap<String, Any>())
    }
}